<template>
  <div class="update-birthdy">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import {updateUserName} from '@/api/user'
import dayJs from 'dayjs'

export default {
  data() {
    return {
      currentDate: new Date(this.value),
      minDate: new Date(1990, 0, 1),
      maxDate: new Date()
    }
  },
  props: ['value'],
  methods: {
    async onConfirm() {
      const birthday = dayJs(this.currentDate).format('YYYY-MM-DD')
      try {
        await updateUserName({
          birthday
        })

        this.$toast('更新成功')
        this.$emit('close')
        this.$emit('input', birthday)

      } catch(err) {
        this.$toast('更新失败')
      }
    }
  }
}
</script>

<style>

</style>
